
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>

#define high 480 //游戏画面尺寸
#define width 640
#define PI 3.14159

int main()
{
    initgraph(width,high); //初始化480*640的绘图窗口
    int center_x,center_y; //中心点的坐标，钟表的中心
    center_x=width/2;
    center_y=high/2;
    int secondlength=width/7; //秒针的长度
    int minutelength=width/6; //分针的长度
    int hourlength=width/5; //时针的长度
    
    int secondend_x,secondend_y; //秒针的终点
    int minuteend_x,minuteend_y; //分针的终点
    int hourend_x,hourend_y; //时针的终点
    float sencondAngle; //秒针对应的角度
    float minuteAngle; //分针对应的角度
    float hourAngle; //时针对应的角度
    
    SYSTEMTIME ti;  //定义变量保存当前的时间
    BeginBatchDraw();
    while(1)
    {
        //绘制一个简易的表盘
        setlinestyle(PS_SOLID,1);
        setcolor(WHITE);
        circle(center_x,center_y,width/4);
        
        //画刻度
        int x,y,i;
        for(i=0;i<60;i++)
        {
            x=center_x+int(width/4.3*sin(PI*2*i/60));
            y=center_y+int(width/4.3*cos(PI*2*i/60));
            
            if(i%15==0)
                bar(x-5,y-5,x+5,y+5);
            else if (i%5==0)
                circle(x,y,5);
            else putpixel(x,y,WHITE);
        }
        outtextxy(center_x-25,center_y+width/6,"我的时钟");
        
        GetLocalTime(&ti); //获取当前的时间
        sencondAngle=ti.wSecond*2*PI/60; //一圈一共2*PI，一圈60秒，一秒钟走过的角度为2*PI/60
        minuteAngle=ti.wMinute*2*PI/60; //一圈一共2*PI，一圈60秒，一秒钟走过的角度为2*PI/60
        hourAngle=ti.wHour*2*PI/60; //一圈一共2*PI，一圈60秒，一秒钟走过的角度为2*PI/60
		
        //由角度决定的秒针端点的坐标
        secondend_x=center_x+secondlength*sin(sencondAngle);
        secondend_y=center_y-secondlength*cos(sencondAngle);
        
        //由角度决定的分针端点的坐标
        minuteend_x=center_x+minutelength*sin(minuteAngle);
        minuteend_y=center_x-minutelength*cos(minuteAngle);
        
        //由角度决定的时针端点的坐标
        hourend_x=center_x+hourlength*sin(hourAngle);
        hourend_y=center_y-hourlength*cos(hourAngle);
        
        setlinestyle(PS_SOLID,2);
        setcolor(WHITE);
        line(center_x,center_y,secondend_x,secondend_y); //画秒针
        
        setlinestyle(PS_SOLID,4);
        setcolor(BLUE);
        line(center_x,center_y,minuteend_x,minuteend_y); //画分针
        
        setlinestyle(PS_SOLID,6);
        setcolor(RED);
        line(center_x,center_y,hourend_x,hourend_y);  //画时针
        FlushBatchDraw();
        Sleep(50);
        setcolor(BLACK);
        setlinestyle(PS_SOLID,2);
        line(center_x,center_y,secondend_x,secondend_y); //隐藏之前的秒针
        setlinestyle(PS_SOLID,4);
        line(center_x,center_y,minuteend_x,minuteend_y); //隐藏之前的分针
        setlinestyle(PS_SOLID,6);
        line(center_x,center_y,hourend_x,hourend_y); //隐藏之前的时针
        
    }
	EndBatchDraw();
    getch();
    closegraph();
    return 0;
}
